package cn.edu.bjtu.toychange.action;

import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

import cn.edu.bjtu.toychange.service.UserService;
import cn.edu.bjtu.toychange.service.impl.UserServiceImpl;
import cn.edu.bjtu.toychange.entity.User;

public class LoginAction extends ActionSupport{

	private static final long serialVersionUID = -3830138203686167366L;
	private String userID;
	private String passWord;
	private UserService userService;
	
	public LoginAction() {
		userService = new UserServiceImpl();
	}
	
	public String execute(){
		User u = userService.getUser(Integer.valueOf(userID));
		
		if (u.equals(null))
			return "login";
		
		if (!passWord.equals(u.getU_password()))
			return "login";
		
		ServletActionContext.getRequest().getSession().setAttribute("currentUser", u);  
		return "index";
	}
	
	/**
	 * structs2拦截器通过下列方法传递值
	 */
	public void setUserID(String userID) {
		this.userID = userID;
	}
	
	public String getUserID() {
		return userID;
	}
	
	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}
	
	public String getPassWord() {
		return passWord;
	}
	
	public UserService getUserService() {
		return userService;
	}
	
	/**
	 * spring通过下列的方法注入userService
	 */
	public void setUserDao(UserService userService) {
		this.userService = userService;
	}
}
